package com.mydubbo.test.hessian;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caucho.hessian.io.SerializerFactory;
import com.caucho.hessian.server.HessianSkeleton;

public class DispatcherServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 951251247182073873L;
	Map<String, HessianSkeleton> map = new HashMap<>();
	SerializerFactory serializerFactory = new SerializerFactory();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("dispatcherServlet接收请求成功");
		String url = req.getRequestURL().toString();
		System.out.println("请求url=>" + url);
		InputStream in = req.getInputStream();
		OutputStream out = resp.getOutputStream();
		System.out.println(map);
		HessianSkeleton hs = map.get(url);
		try {
			hs.invoke(in, out, serializerFactory);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		HessianSkeleton userHS = new HessianSkeleton(new UserServiceImpl(), UserService.class);
		map.put("/service/user", userHS);
		System.out.println(map);
	}

}
